package com.aramco.fion.controller
{
	import com.aramco.fion.event.BaseEvent;
	
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	
	public class BaseController
	{
		
		
		private namespace self;
		
		private var viewHandlers  : IResponder = null;
		
		public function cacheCaller(event:BaseEvent):void {
			viewHandlers = getResponderFor(event);
		}
		
		
		public function notifyCaller(results:* = null):void {
			/*
			Default result handler simply forwards 
			the event to the view handler... if available
			Note: the results == FaultEvent or ANY data type
			this allows data to be directly delivered to the caller without
			a ResultEvent wrapper
			
			!! We forward the handling to this classes fault/result handlers
			not a subclass overrides
			*/
			if (results is FaultEvent) self::fault(results);
			else                                       self::result(results);
		}
		
		private function getResponderFor(event:BaseEvent):IResponder {
			var results : IResponder = null;
			if (event != null) {
				//	if (event is BaseEvent){
				results = event.callbacks;
				//	}
			}
			return results;
		}
		
		self function result(info:Object):void {
			if (viewHandlers && (viewHandlers.result != null)) {
				viewHandlers.result(info);
			}
		}
		
		self function fault( info:Object ) : void {
			if (viewHandlers && (viewHandlers.fault != null)) {
				viewHandlers.fault(info);
			}
		}
	}
}